<template>
  <div class="order-buy">
    <div class="order-con">
      <!-- 地址管理 -->
      <address-admin ref="addressAdmin"
                     @openSelfAddress="openSelfAddress"
      >
      </address-admin>
    </div>
    <!-- 商品信息 -->
    <goods-info :buyLists="buyLists"></goods-info>
    <section class="item-block item-block2">
      <section class="money-box">
        <p class="msettlement_intle">
          <span>总计：</span>
          <span class="menoy-num"  v-html="scaleGoodsPriceFn(totalPrice)"></span>
        </p>
      </section>
    </section>
    <!-- 支付按钮 -->
    <section class="msettlement_sub">
      <div class="btn-addorder red-gra-btn" :class="isSubmit?'no-click':''" @click="clickSubmitOrder">提交订单
      </div>
    </section>
    <div class="paypopnote" v-show="isSubmit"><span>正在提交中……</span></div>
  </div>

</template>

<script>
import {
  getOrderBuyData,
  addOrder
} from '@/api/order/order.js'
import addressAdmin from '@/views/order/buy/components/addressAdmin/index.vue'
import goodsInfo from '@/views/order/buy/components/goodsInfo/index.vue'
import Store from '@/utils/store'
import { scaleGoodsPrice } from '@/utils/index.js'

export default {
  components: {
    addressAdmin,
    goodsInfo
  },
  data() {
    return {
      resData: {},
      buyLists: [], // 商品数组
      itemIds: [], // 商品id数组
      sku_id: [],
      sku_name: [],
      current_price: [],
      is_first_gift: [],
      num: [],
      active: [],
      buy_gift_type: [],
      dollar_price: [],
      sup_post_fee: [],
      new_supplier_id: [],
      supplier_ids: [],
      isSubmit: false,
      totalPrice: 0.00, // 商品总价
      isOrder: false,
      formData: {
        'is_dhs_send': false,
        'id_cert_name': '',
        'id_card': '',
        'e_mail': '',
        'is_freight_collect': 0,
        'is_freight_insurance': 0,
        'global_alipay_currency': '',
        'hantepay': '',
        'allinpay': '',
        'newyeepay': '',
        'shipping_type': '',
        'latitude': undefined,
        'longitude': undefined,
        'addressLatitude': undefined,
        'addressLongitude': undefined,
        'money_type': '',
        'user_coupon_id': '',
        'user_sup_coupon_id': '',
        'max_coupon_value': '',
        'allow_coupon_items': '',
        'type': '',
        'id_pros': '',
        'id_cons': '',
        'custom_pic': [],
        'address_id': '',
        // 已选地址有没有经纬度
        'hasLatLng': false,
        'self_address_id': '',
        'user_realname': '',
        'user_mobile': '',
        'receiver_name_dz': '',
        'receiver_mobile_dz': '',
        'is_invoice_type': 1,
        'invoice_type': '',
        'invoice_no': '',
        'mail': '',
        'invoice_header': '',
        'company_address': '',
        'company_phone': '',
        'open_bank': '',
        'bank_account': '',
        'coupon_code': '',
        'coupon_code_price': '',
        'coupon_code_name': '',
        'message': '',
        'dz_balance': 0, // 薇曼定制价格
        'dz_online': 0,
        'serve_address_id': '',
        'is_store_serve': '',
        'mnnk_img': '', // 蒙牛定制
        'mnnk_remark': '', // 蒙牛定制
        'is_show_mnnk': 0, // 蒙牛定制
        'list_shipping_id': 0,
        'virtual_username': '',
        'virtual_phone': '',
        'gift_cards_str': ''
      }
    }
  },
  methods: {
    init() {
      const data = Store.fetch('orderBuy')
      this.getData(getOrderBuyData, data)
    },
    // 点击选择自提地址
    openSelfAddress(addressData) {
      // // 门店配送选择门店配送地址时获取收货地址的经纬度
      // if (this.formData.shipping_type == 5 && addressData.latitude && addressData.latitude != 0) {
      //   this.formData.addressLatitude = addressData.latitude
      //   this.formData.addressLongitude = addressData.longitude
      // }
      // // 调用子组件方法
      // if (this.$refs.modeDistribution) {
      //   this.$refs.modeDistribution.selfAddressVisible = false
      //   this.$refs.modeDistribution.$refs.selfAddressDialog.dialogVisible = false
      //   this.$refs.modeDistribution.$refs.selfAddressDialog.dialogVisible = true
      // }
    },
    JumpName(url, data) {
      this.$JumpName(this, url, data)
    },
    scaleGoodsPriceFn(price) {
      return scaleGoodsPrice(price)
    },
    // 订单结算
    getData(method, data) {
      this.$loadingWrap.show()
      if (this.$route.query.gift_cards_str) {
        data.gift_cards_str = this.$route.query.gift_cards_str
        this.formData.gift_cards_str = this.$route.query.gift_cards_str
      }
      method(data).then(res => {
        this.$loadingWrap.close()
        if (res.status == 1) {
          const resData = res.data
          this.resData = res.data
          this.user_info = resData.user_info
          if (this.user_info) {
            this.formData.virtual_username = this.user_info.name || ''
            this.formData.virtual_phone = this.user_info.mobile || ''
          }

          // form提交数据
          this.formData.is_freight = resData.is_freight
          if (resData.default_shipping_type) {
            for (const key in resData.list_shipping) {
              if (key == resData.default_shipping_type) {
                this.formData.shipping_type = resData.default_shipping_type
              }
            }
          }
          this.buyLists = resData.buy_lists
          this.itemIds = []
          this.sku_id = []
          this.sku_name = []
          this.current_price = []
          this.is_first_gift = []
          this.num = []
          this.active = []
          this.buy_gift_type = []
          this.dollar_price = []
          this.sup_post_fee = []
          this.new_supplier_id = []
          this.supplier_ids = []
          this.buyLists.forEach((item, indx) => {
            this.itemIds.push(item.item_id)
            this.sku_id.push(item.sku_id)
            this.sku_name.push(item.props)
            this.current_price.push(item.price)
            this.is_first_gift.push(item.is_first_gift)
            this.num.push(item.num)
            this.active.push(item.active)
            this.buy_gift_type.push(item.buy_gift_type)
            this.dollar_price.push(item.dollar_price)
            this.sup_post_fee.push(item.sup_post_fee)
            this.new_supplier_id.push(item.new_supplier_id)
            if (item.new_supplier_id) {
              this.supplier_ids.push(item.new_supplier_id)
            }
          })
          this.formData.list_shipping_id = resData.list_shipping_id

          this.formData.is_dhs_send = resData.is_dhs_send
          this.formData.id_pros_cons = resData.id_pros_cons
          this.formData.id_pros_cons_one = resData.id_pros_cons_one
          this.formData.is_buy_card = resData.is_buy_card
          this.formData.is_custom_pic = resData.custom_pic
          this.formData.is_one_item = 1
          this.formData.order_source = resData.order_source
          this.formData.live_id = resData.live_id
          this.formData.assort_id = resData.assort_id
          this.formData.item_cycle_buy_id = resData.item_cycle_buy_id
          this.formData.item_cycle_sku = resData.item_cycle_sku
          this.formData.item_cycle_delivery = resData.item_cycle_delivery
          this.formData.is_send_gift = resData.send_gift
          this.formData.max_coupon_value = resData.max_coupon_value
          this.formData.allow_coupon_items = resData.allow_coupon_items
          this.formData.cart_ids = resData.cart_ids
          this.formData.is_user_temp = resData.is_user_temp
          this.formData.receiver_name_dz = resData.receiver_name_dz
          this.formData.receiver_mobile_dz = resData.receiver_mobile_dz
          this.formData.dollar_payment = resData.dollar_payment
          this.formData.code = resData.code
          this.formData.deny_coupon_items = resData.deny_coupon_items
          this.formData.item_miao_id = resData.item_miao_id
          this.formData.item_bargin_id = resData.item_bargin_id
          this.formData.is_store_fn_pt = resData.is_store_fn_pt
          this.formData.scanCode = resData.scanCode
          this.formData.addressId = resData.addressId
          this.formData.streetBtn = resData.is_select_town
          this.formData.total_price = resData.payment
          this.formData.post_price = resData.post_fee
          this.formData.is_point_limit_money = resData.is_point_limit_money
          this.formData.point_cash = resData.point_cash
          this.formData.point_payment_hidden = resData.point_payment
          this.formData.user_points_hidden = resData.user_points
          this.formData.point_expense_way = resData.point_expense_way
          this.formData.is_point_expense_way = resData.is_point_expense_way
          this.formData.point_name = resData.point_name
          this.formData.virtual_cash = resData.virtual_cash
          this.formData.virtual_payment_hidden = resData.virtual_currency_fee
          this.formData.user_virtual_hidden = resData.virtual_currency_num
          this.formData.balance_price = resData.user_balance
          this.formData.must_pay_password = resData.must_pay_password
          this.formData.item_total_num = resData.item_total_num
          this.formData.item_total_weight = resData.item_total_weight
          this.formData.item_total_volume = resData.item_total_volume
          this.formData.freight_tpl_id = resData.freight_tpl_id
          this.formData.group_shopping_id = resData.group_shopping_id
          this.formData.freight_tpl_payment = resData.freight_tpl_payment
          this.formData.point_cash_limit_percent = resData.point_cash_limit_percent
          this.formData.virtual_cash_max_percent = resData.virtual_cash_max_percent
          this.formData.json_real_price = resData.json_real_price
          this.formData.package_active_diff_price = resData.package_active_diff_price
          this.formData.full_mail_is_price = resData.full_mail_is_price
          this.formData.select_last_location = resData.select_last_location
          this.formData.is_id_cert = resData.is_id_cert
          this.is_dataset_json = resData.is_dataset_json
          
          this.formData.item_id = this.itemIds
          this.formData.sku_id = this.sku_id
          this.formData.sku_name = this.sku_name
          this.formData.current_price = this.price
          this.formData.current_dollar_price = this.dollar_price
          this.formData.sup_post_fee = this.sup_post_fee
          this.formData.new_supplier_id = this.new_supplier_id
          this.formData.is_first_gift = this.is_first_gift
          this.formData.buy_gift_type = this.buy_gift_type
          this.formData.supplier_id = this.supplier_ids
          this.formData.active = this.active
          this.formData.num = this.num

          this.formData.supplier = Store.fetch('orderBuy').supplier
          this.formData.is_store_serve = resData.is_store_serve
          this.formData.is_show_mnnk = this.is_show_mnnk


          const arrDate = []
          const arrMeno = []

          if (this.order_extry) {
            arrDate.push({ 'item_date': this.order_extry.item_date })
            arrMeno.push({ 'item_memo': this.order_extry.item_memo })
            if (this.is_anh_dz == 1) {
              this.formData.order_extry_date_json = arrDate
              this.formData.order_extry_memo_json = arrMeno
            } else {
              this.formData.order_extry_date = arrDate
              this.formData.order_extry_memo = arrMeno
            }
          }
          

          if (resData.address[0]) {
            if (this.resData.is_buy_card == 1) {
              this.formData.id_card = resData.address[0].id_card
            }
            if (this.resData.is_buy_card == 1 && this.resData.is_id_cert_name == 1) {
              this.formData.id_cert_name = resData.address[0].id_cert_name
            }
            this.formData.e_mail = resData.address[0].email
            this.formData.hasLatLng = !!(resData.address[0].latitude && resData.address[0].longitude)
          }
          this.formData.user_realname = resData.self_user_info.name
          this.formData.user_mobile = resData.self_user_info.mobile

          this.formData.province_id = resData.province_id
          this.formData.area_id = resData.area_id
          this.formData.city_id = resData.city_id
          // this.getPostFee(true)
        } else {
          this.$Error(res.msg)
          setTimeout(() => {
            if (res.data && !res.data.redirect_url) {
              this.$router.go(-1)
            }
          }, 1000)
        }
      })
    },
    getPostFee() {
    },
    // 提交订单
    clickSubmitOrder() {
      if(this.formData.address_id == '') {
        this.$Error('请选择收货地址')
        return
      }
      this.isSubmit = true
      addOrder(this.formData).then(res => {
        if (res.status == 1) {
          this.JumpName('/order-lists')
          setTimeout(() => {
            this.isSubmit = false
            this.isOrder = false
          }, 1000)
        } else {
          this.isSubmit = false
          this.isOrder = false
          this.$Error(res.msg)
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1000)
        }
      })
    }
  },
  created() {
    this.init()
  }
}
</script>

<style lang="scss">
.prescription-info-wrapper {
  padding: 30px 20px;
  background: #FEF2E4;
  margin: 0 30px 24px;
  border-radius: 20px;

  .prescription-info {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .left {
      padding-left: 20px;
      border-left-style: solid;
      border-left-width: 10px;

      &.success {
        border-left-color: #03C28A;
      }

      &.warning {
        border-left-color: #FF702C;
      }

      .title {
        font-size: 32px;
        font-weight: bold;
      }

      .desc {
        font-size: 24px;
        margin-top: 16px;
        color: #999;
      }
    }

    .right {
      font-size: 24px;
      color: #999;
      flex-shrink: 0;
    }
  }

  .warning {
    color: #FF702C
  }

  .success {
    color: #03C28A
  }
}
</style>

<style lang="scss">
@import "../../../styles/mixin";
@import "src/styles/order/orderBuy";

.pchasingTipConfirm {
  .van-dialog__footer {
    padding: 0 60px 48px;

    .van-dialog__confirm {
      height: 80px;
    }
  }
}

.cashPoint {
  border-bottom: none !important;
}

.exchangeFee {
  color: #B6B6B6;
  line-height: 50px;
  border-bottom: 1px solid #EDEDED;
}

.lzt-flex {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;

  div {
    width: 45%
  }
}

.lzt-pop {
  width: 90%;
  min-height: 200px;
  border-radius: 20px;
  text-align: center;
  padding: 40px;
  line-height: 40px;

  .behalf-others {
    margin-top: 40px;

    .use-button {
      background: #EC2213;
      font-size: 28px;
      color: #fff;
      border: none;
      height: 50px;
      line-height: 50px;
      padding: 0 20px;
      border-radius: 10px;
      margin-left: 20px;
    }
  }
}

.item-block.item-block2 {
  .item-box {
    .custom-pic-title.item-title {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      height: auto;

      .flex-shrink {
        flex-shrink: 0;
      }

      .cunstom-text {
        line-height: 1.2;
        height: auto;
        margin-left: 20px;
        padding: 18px 0;
      }
    }
  }
}

.kuajingPrice {
  font-size: 28px !important;

  span {
    font-size: 32px !important;
    font-weight: 500;
  }
}

.no-click {
  pointer-events: none;
  background: #ccc;
}

// 定制 赠品
.gift-list {
  padding: 20px;

  .title {
    font-size: 28px;
    font-weight: 500
  }

  .gift-sku {
    .small-title {
      margin: 20px 0 10px 0;
    }

    .gift-dz {
      display: flex;
      flex-wrap: wrap;

      div {
        background: #F2F2F2;
        border: 1px solid #F2F2F2;
        margin-bottom: 20px;
        color: #333;
        font-size: 24px;
        padding: 2px 30px;
        border-radius: 50px;
        margin-right: 24px;

        &.click-gift-sku {
          background: #F21228;
          border: 1 p solid #F21228;
          color: #fff
        }

      }
    }
  }
}

.purchase_agreement-checkbox {
  border-bottom: 2px solid #c8c9cc;
}
.text-tips {
  font-size: 24px;
  // margin-bottom: 30px;
  padding: 20px 0;
  color: #F30C23;
  text-align: center;
  background: transparent;
}
</style>
